﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;

public partial class Controls_personal : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            ShowInformation();
    }
    protected void ShowInformation()
    {
        DBaccess db = new DBaccess();
            if (Session["gid"].ToString() == "2")
            {
                SqlDataReader read = db.GetReader("select mname,score,last_resource_time,gid from mr_member,mr_student where mr_member.mid=mr_student.sid and mid='" + Session["mid"].ToString() + "'");
                read.Read();
                Label1.Text = read.GetValue(0).ToString();
                Label3.Text = read.GetValue(1).ToString();
                DataSet ds = db.GetDataSet("select logo from mr_member where mid='" + Session["mid"].ToString() + "'");
                ImageButton1.ImageUrl = ResolveUrl(ds.Tables[0].Rows[0]["logo"].ToString());
                string last_resource_time = read.GetValue(2).ToString();
                SqlDataReader read1 = db.GetReader("select count(*) from mr_announcement where time>'" + last_resource_time + "'");
                read1.Read();
                if (read1.GetValue(0).ToString() != "0")
                    Button1.Text = "你有" + read1.GetValue(0).ToString() + "条新公告";
                else
                    Button1.Text = "你有0条新公告";

                HyperLink2.NavigateUrl = "./ChangePassword.aspx";
            }
            else if (Session["gid"].ToString() == "1" || Session["gid"].ToString() == "3")
            {
                SqlDataReader read1 = db.GetReader("select mname,last_resource_time from mr_member where mid='" + Session["mid"].ToString() + "'");
                read1.Read();
                Label1.Text = read1.GetValue(0).ToString();
                Label2.Visible = false;
                Label3.Visible = false;
                DataSet ds = db.GetDataSet("select logo from mr_member where mid='" + Session["mid"].ToString() + "'");
                ImageButton1.ImageUrl = ResolveUrl(ds.Tables[0].Rows[0]["logo"].ToString());
                string last_resource_time = read1.GetValue(1).ToString();
                SqlDataReader read2 = db.GetReader("select count(*) from mr_announcement where time>'" + last_resource_time + "'");
                read2.Read();
                if (read1.GetValue(0).ToString() != "0")
                    Button1.Text = "你有" + read2.GetValue(0).ToString() + "条新公告";
                else
                    Button1.Text = "你有0条新公告";
                if (Session["gid"].ToString() == "1")
                    HyperLink2.NavigateUrl = "../Teacher/ChangePassword.aspx";
                else if (Session["gid"].ToString()== "3")
                    HyperLink2.NavigateUrl = "../Management/ChangePassword.aspx";
            }
        HyperLink1.NavigateUrl = "../Login.aspx";
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if(Session["mid"].ToString() == "2")
            Response.Redirect("./ChangeInformation.aspx");
        else if(Session["mid"].ToString() == "1")
            Response.Redirect("./ChangeInformation.aspx");
        else if(Session["mid"].ToString() == "3")
            Response.Redirect("./ChangeInformation.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DBaccess db = new DBaccess();
        SqlDataReader read = db.GetReader("select last_resource_time from mr_member,mr_student where mr_member.mid=mr_student.sid and mid='" + Session["mid"].ToString() + "'");
        read.Read();
        string last_resource_time = read.GetValue(0).ToString();
        SqlDataReader read1 = db.GetReader("select announcement from mr_announcement where time>'" + last_resource_time + "'");
        read1.Read();
        string ann = read1.GetValue(0).ToString();
        Response.Write("<script>alert(\"" + ann + "\");</script>");
        //StringBuilder sb = new StringBuilder();
        //sb.Append("<script language=\"javascript\"> \n");
        //sb.Append("alert(\"" + ann.Trim() + "\"); \n");
        //sb.Append("</script>");
    }
}